package com.biz.primus.ms.commodity.dao.redis;

import com.biz.primus.base.enums.SaleChannel;
import com.biz.primus.base.redis.CrudRedisDao;
import com.biz.primus.base.redis.util.RedisUtil;
import com.biz.primus.commodity.vo.CategoryProductSalesTopVo;
import com.biz.primus.commodity.vo.backend.ProductSalesVo;
import com.biz.primus.common.utils.ValueUtils;
import com.biz.primus.ms.commodity.dao.ro.CategoryRo;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

/**
 * 分类 RedisDao
 *product:CategoryRo:337063315824054273:TopProduct
 * @author david-liu
 * @date 2017年01月04日
 * @reviewer
 * @see
 */
@Repository
public class CategoryRedisDao extends CrudRedisDao<CategoryRo, Long> implements Serializable {

    private static final long serialVersionUID = 1009646058514234622L;


    /**
     * 获取分类TopN sortSet Key
     *
     * @param categoryId
     * @return
     */
    public String getCategorySalesTopSortSetKey(Long categoryId) {
        return this.getKeyByParams(categoryId, "TopProduct");
    }

    /**
     * APP 分类
     * @return
     */
    public String appCategorySortSetKey(){
        return this.getKeyByParams("App");
    }

    public void setAppCategorySort(Long categoryId, Double sort){
        zadd(appCategorySortSetKey(), sort, RedisUtil.toByteArray(categoryId));
    }

    public void removeCategoryFromAppSort(Long categoryId){
        if (categoryId == null){
            return;
        }
        zrem(appCategorySortSetKey(),RedisUtil.toByteArray(categoryId));
    }

    public List<CategoryRo> listAppCategoryBySort(){
        return findByIds(zRange(appCategorySortSetKey(), 0, -1));
    }

    /**
     * 设置topN分类排行商品
     * @param lists
     */
    public void setCategorySalesTopN(List<CategoryProductSalesTopVo> lists) {
        for (CategoryProductSalesTopVo index : lists) {
            for (ProductSalesVo productSalesVo:index.getProductSalesVos()){
                zadd(getCategorySalesTopSortSetKey(index.getCategoryId()),productSalesVo.getQuantity(),
                        RedisUtil.toByteArray(productSalesVo.getProductCode()));
            }
        }
    }

    public List<String> getCategorySalesTopN(Long categoryId, Integer size){
        Set<byte[]> set = zrevrange(getCategorySalesTopSortSetKey(categoryId),0,-1);
        List<String> resultList = RedisUtil.bytesSetToStringList(set);
        if (resultList != null && resultList.size() > size*2){
            resultList = resultList.subList(0,size*2);
        }
        return resultList;
    }

    @Override
    public void save(CategoryRo categoryRo){
        super.save(categoryRo);
        if (categoryRo.getSaleChannel() == SaleChannel.ALL || categoryRo.getSaleChannel() == SaleChannel.APP){
            this.setAppCategorySort(categoryRo.getId(),Double.valueOf(ValueUtils.getValue(categoryRo.getIdx())));
        }else {
            this.removeCategoryFromAppSort(categoryRo.getId());
        }
    }

    @Override
    public void save(Iterable<CategoryRo> categoryRos){
        super.save(categoryRos);
        for (CategoryRo categoryRo: categoryRos){
            this.save(categoryRo);
        }
    }

}
